// 选择时间和日期
$(".form-date").datetimepicker({
    weekStart: 1,
    todayBtn:  1,
    autoclose: 1,
    todayHighlight: 1,
    startView: 2,
    forceParse: 0,
    showMeridian: 1,
    format: "yyyy-mm-dd hh:ii:ss"
});
// 添加表单验证
$(document).ready(function() {
    // 修改表单验证
    $('#editForm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            userName: {
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    }
                }
            },
            passWord: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {
                        message: '手机号不能为空'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: '邮箱不能为空'
                    },
                    emailAddress: {
                        message: '邮箱地址格式有误'
                    }
                }
            }
        }
    })
        .on('success.form.bv', function (e) {
            // Prevent form submission
            e.preventDefault();
            // Get the form instance
            var $form = $(e.target);
            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');
            // Use Ajax to submit form data
            $.post($form.attr('action'), $form.serialize(), function(result) {
                console.log(result);
                if (result.errorCode == 'y') {
                    new $.zui.Messager('提示消息：' + result.errorText, {
                        icon: 'exclamation-sign',
                        type: 'success',
                        time: 2000
                    }).show();
                    location.reload(true);
                } else {
                    new $.zui.Messager('提示消息：' + result.errorText, {
                        icon: 'exclamation-sign',
                        type: 'success',
                        time: 2000
                    }).show();
                }
            }, 'json');
        });
    // 取消
    $('#editResetBtn').click(function() {
        $('#editForm').data('bootstrapValidator').resetForm(true);
    });
});
// 全选
function checkBox(name,checked){
    //全选开始
    $("input[name='"+name+"']").prop("checked", checked);
}
// 修改
function optUpdate() {
    // 首先获取所有选中的id集合
    var check = $(".checked_detect:checked");
    // 定义一个存放id集合的数组
    var ids = new Array();
    for (var i=0; i < check.length; i++) {
        ids[i] = $(check[i]).attr("value");
    }
    // 判断是否选择了一个元素
    if (ids.length == 0) {
        new $.zui.Messager('提示消息：' + "请至少选择一个(且只能选择一项)", {
            icon: 'exclamation-sign',
            type: 'danger',
            time: 2000
        }).show();
        return;
    } else if (ids.length > 1) {
        new $.zui.Messager('提示消息：' + "请至少选择一个(且只能选择一项)", {
            icon: 'exclamation-sign',
            type: 'danger',
            time: 2000
        }).show();
        return;
    }
    bootbox.confirm({
        title: "操作提示",
        size: "small",
        message: "你确定要修改选择的数据吗？",
        callback: function (result) {
            if (result) {
                $.ajax({
                    type : "GET",
                    cache : false,
                    url : "/admin/member/get/" + ids,
                    contentType: "application/json;charset=utf-8",
                    dataType:"json",
                    success : function (result) {
                        if (result.errorCode == 'y') {
                            $("#id").val(result.memberDto.id);
                            $("#name").val(result.memberDto.name);
                            $("#passWord").val(result.memberDto.passWord);
                            $("#phone").val(result.memberDto.phone);
                            $("#email").val(result.memberDto.email);
                            $('#editModal').modal({
                                keyboard : false,
                                show     : true
                            });
                        } else {
                            new $.zui.Messager('提示消息：' + result.errorText, {
                                icon: 'exclamation-sign',
                                type: 'danger',
                                time: 2000
                            }).show();
                        }
                    },error:function(){
                        new $.zui.Messager('提示消息：' + "系统异常", {
                            icon: 'exclamation-sign',
                            type: 'danger',
                            time: 2000
                        }).show();
                    }
                });
            }
        }
    });
}
// 删除
function optDelete() {
    // 首先获取所有选中的id集合
    var check = $(".checked_detect:checked");
    // 定义一个存放id集合的数组
    var ids = new Array();
    for (var i=0; i < check.length; i++) {
        ids[i] = $(check[i]).attr("value");
    }
    // 判断是否选择了一个元素
    if (ids.length == 0) {
        new $.zui.Messager('提示消息：' + "请至少选择一个", {
            icon: 'exclamation-sign',
            type: 'danger',
            time: 2000
        }).show();
        return;
    }
    // 删除确认
    bootbox.confirm({
        title: "操作提示",
        size: "small",
        message: "你确定要删除选择的数据吗？",
        callback: function (result) {
            if (result) {
                $.ajax({
                    type : "POST",
                    cache : false,
                    url : "/admin/member/deleteBatch",
                    data : {
                        ids :ids
                    },
                    contentType: "application/x-www-form-urlencoded;charset=utf-8",
                    dataType:"json",
                    success : function (result) {
                        if (result.errorCode == 'y') {
                            new $.zui.Messager('提示消息：' + result.errorText, {
                                icon: 'exclamation-sign',
                                type: 'success',
                                time: 2000
                            }).show();
                            setTimeout("location.reload()", 1000);
                        } else {
                            new $.zui.Messager('提示消息：' + result.errorText, {
                                icon: 'exclamation-sign',
                                type: 'danger',
                                time: 2000
                            }).show();
                        }
                    },error : function(result) {
                        new $.zui.Messager('提示消息：' + result.errorText, {
                            icon: 'exclamation-sign',
                            type: 'danger',
                            time: 2000
                        }).show();
                    }
                });
            }
        }
    });
}
// 分页函数
function queryAll(pageNum, pageSize){
    var searchParam = $("input[name='searchParam']").val();
    var startDate = $("input[name='startDate']").val();
    var endDate = $("input[name='endDate']").val();
    document.location.href="/admin/member/list?pageNum="+pageNum+"&searchParam="+searchParam+"&startDate="+startDate+"&endDate="+endDate;
}